package cn.shopping.controller.aclassification;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.annotations.Param;
import org.aspectj.apache.bcel.generic.LSTORE;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.shopping.pojo.Sh_one;
import cn.shopping.pojo.Sh_three;
import cn.shopping.pojo.Sh_two;
import cn.shopping.service.classification.ClassificationService;


@Controller
@RequestMapping("/classification")
public class ClassificationController {

	@Resource
	private ClassificationService classificationService;
	
	
	/**
	 * 一级分类显示
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/One_ClassificationList.html",method=RequestMethod.GET)
	public String One_ClassificationList(Model model){
		List<Sh_one> ClassificationList = classificationService.getClassificationlist();
		model.addAttribute("list", ClassificationList);
		return "yijifenlei";
	}
	
	/**
	 * 跳转一级分类
	 * @return
	 */
	@RequestMapping(value="/toadd_One_Classificatio.html",method=RequestMethod.GET)
	public String toadd_One_Classification(){
		return "tianjiayijifenlei";
	}
	/**
	 * 添加一级分类
	 * @param one
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/add_One_Classificatio.html",method=RequestMethod.POST)
	public String add_One_Classification(Sh_one one,Model model){
		if (One_Name(one.getOne_name())==0) {
			if (classificationService.add_One_Classificatio(one)==1) {
				return "redirect:/classification/One_ClassificationList.html";
			}
			model.addAttribute("error", "添加失败");
		}
		model.addAttribute("error", "名称重复");
		return "tianjiayijifenlei";
	}
	
	/**
	 * 删除
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/del_One_Classification.html" ,method=RequestMethod.GET)
	public String del_One_Classification(@RequestParam("id") Integer id, Model model){
		if (classificationService.del_One_Classification(id)==1) {
			return "redirect:/classification/One_ClassificationList.html";
		}
		model.addAttribute("error","删除失败");
		return "redirect:/classification/One_ClassificationList.html";
	}
	
	/**
	 * 去到修改页面
	 * @return
	 */
	@RequestMapping(value="/to_update_One_Classification.html",method=RequestMethod.GET)
	public String to_update_One_Classification(@RequestParam("id") Integer id ,Model model){
		Sh_one one = classificationService.get_One_Classification(id);
		model.addAttribute("one",one);
		return "xiugaiyijifenlei";
	}
	
	
	/**
	 * 修改
	 * @return
	 */
	@RequestMapping(value="/update_One_Classification.html",method=RequestMethod.POST)
	public String update_One_Classification(
			@RequestParam String one_name,
			@RequestParam int id,
			Model model,Sh_one one){
		one.setId(id);
		one.setOne_name(one_name);
		model.addAttribute(one);
		if (classificationService.update_One_Classification(one)==1) {
			return "redirect:/classification/One_ClassificationList.html";
		}
		model.addAttribute("error","修改失败");
		return "xiugaiyijifenlei";
	}
	
	
	@RequestMapping(value="/to_add_two_Classification.html",method=RequestMethod.GET)
	public String to_add_two_Classification(@RequestParam("id") Integer id ,Model model){
		Sh_two two = classificationService.get_two_Classification(id);
		model.addAttribute("two", two);
		return "tianjiaerjifenlei";
	}
	
	
	@RequestMapping(value="/add_two_Classification.html",method=RequestMethod.POST)
	public String add_two_Classification(
			@RequestParam(value="id") Integer id,
			@RequestParam(value="two_name")String two_name, 
			Sh_two two ,
			Model model){
		if (!two_name.isEmpty()) {
			if (classificationService.add_two_Classification(two)==1) {
				return "redirect:/classification/two_Classificationlist.html";
			}
			model.addAttribute("error", "添加失败");
			return "tianjiaerjifenlei";
		}else{
			model.addAttribute("error", "名称不能为空");
		}
		return "tianjiaerjifenlei";
	}
	
	/**
	 * 判断是否唯一
	 * @param one_name
	 * @return
	 */
	private int One_Name(@RequestParam String one_name){
		int num = classificationService.getName(one_name);
		if (num==1) {
			return 1;
		}
		return 0;
	}
	
	
	//
	//
	//二级分类
	
	
	//查看
	@RequestMapping(value="/two_Classificationlist.html")
	public String two_Classificationlist(Model model,Sh_two two){
		List<Sh_two> list = classificationService.two_Classificationlist();
		model.addAttribute("list" ,list);
		return "erjifenlei";
	}
	
	//删除
	@RequestMapping(value="/del_two_Classificationlist.html")
	public String del_two_Classificationlist(@RequestParam("two_id")Integer two_id,Model model ){
		if (classificationService.del_two_Classificationlist(two_id)==1) {
			return "redirect:/classification/two_Classificationlist.html";
		}
		 model.addAttribute("error","删除失败");
		return "redirect:/classification/two_Classificationlist.html";
		
	}
	
	//跳转修改二级分类
	@RequestMapping(value="/to_update_two_Classificationlist.html",method=RequestMethod.GET)
	public String to_update_two_Classificationlist(
			@RequestParam("two_id") Integer two_id,
			Model model ,Sh_two two){
		two = classificationService.get_two_id_Classification(two_id);
		model.addAttribute("two", two);
		return "xiugaierjifenlei";
	}
	
	//修改二级分类
	@RequestMapping(value="/update_two_Classification.html",method=RequestMethod.POST)
	public String update_two_Classification(
			@RequestParam Integer two_id,
			@RequestParam String two_name,Sh_two two){
		if (classificationService.update_two_Classification(two)==1) {
			
			return "redirect:/classification/two_Classificationlist.html";
		}
		return "xiugaierjifenlei";
	}
	
	//跳转添加三级分类页面
	@RequestMapping(value="/to_add_three_Classification.html",method=RequestMethod.GET)
	public String to_add_three_Classification(@RequestParam Integer two_id,Model model,Sh_three three){
		three = classificationService.get_tow_id_add_three(two_id);
		model.addAttribute("three",three);
		return "tianjiasanjifenl";
	}
	
	//添加三级分类
	@RequestMapping(value="/add_three_Classification.html",method=RequestMethod.POST)
	public String add_three_Classification(
			@RequestParam(value="two_id") Integer two_id,
			@RequestParam(value="three_name") String three_name,Sh_three three,Model model){
		if (classificationService.add_three_Classification(three)==1) {
			return "redirect:/classification/two_Classificationlist.html";
		}
		
		return "tianjiasanjifenl";
	}	
	
	
	/**
	 * 
	 * 
	 * 三级分类
	 * 
	 * 
	 * @return
	 */
	
	//查看
	@RequestMapping(value="/three_Classification.html")
	public String three_Classification(Model model){
		List<Sh_three> list = classificationService.three_Classification();
		model.addAttribute("list", list);
		return "sanjifenlei";
	}
	
	//删除
	@RequestMapping(value="/del_three_Classification.html")
	public String del_three_Classification(@RequestParam(value="three_id") Integer three_id,Model model){
		if (classificationService.del_three_Classification(three_id)==1) {
			
			return "redirect:/classification/three_Classification.html";
		}
		return "redirect:/classification/three_Classification.html";
	}
	
	@RequestMapping(value="/to_update_three_Classification.html")
	public String to_update_three_Classification(
			@RequestParam(value="three_id") Integer three_id,
			Sh_three three,Model model){
		three = classificationService.get_three_Classification(three_id);
		model.addAttribute("three", three);
		return "xiugaiasanjifenli";
	}
	
	//编辑
	@RequestMapping(value="/update_three_Classification.html")
	public String update_three_Classification(
			@RequestParam String three_name, 
			@RequestParam Integer three_id, 
			Sh_three three,Model model){
		if (classificationService.count_three_name(three_name)!=1) {
			if (classificationService.update_three_Classification(three)==1) {
				return "redirect:/classification/three_Classification.html";
			}else{
				model.addAttribute("error", "添加失败");
			}
		}
		return "xiugaiasanjifenli";
	}
	
}
